package mosync.mobilelualiveditor.ui;

import java.util.*;

public class WidgetInstance
{
	public WidgetInstance(WidgetType type, String name)
	{
		this.mType = type;
		this.mName = name;
		this.mParent = null;
		mProperties = new TreeMap<String, WidgetPropertyInstance>();
		mChildren = new ArrayList<WidgetInstance>();

		ArrayList<WidgetPropertyType> types = type.getPropertyTypes();
		for (WidgetPropertyType t : types)
		{
			mProperties.put(t.mName, new WidgetPropertyInstance(t, this));
		}
	}

	void setProperty(String property, String value)
	{
		mProperties.get(property).setValue(value);
	}

	String getProperty(String property)
	{
		return mProperties.get(property).getValue();
	}

	public Map<String, WidgetPropertyInstance> getProperties()
	{
		return mProperties;
	}

	public void addChild(WidgetInstance child)
	{
		mChildren.add(child);
		child.mParent = this;
	}

	public ArrayList<WidgetInstance> getChildren()
	{
		return mChildren;
	}

	String quote(String s)
	{
		return "\"" + s + "\"";
	}

	private String makeTabs(int n)
	{
		String ret = "";
		for (int i = 0; i < n; i++)
			ret += "\t";
		return ret;
	}

	public String toXML(int numTabs)
	{
		String ret = "";
		ret += makeTabs(numTabs) + "<Widget type=\"" + mType.getName()
			+ "\" name=\"" + mName + "\">\n";

		for (WidgetPropertyInstance p : mProperties.values())
		{
			ret += p.toXML(numTabs + 1);
		}

		for (WidgetInstance c : mChildren)
		{
			ret += c.toXML(numTabs + 1);
		}
		ret += makeTabs(numTabs) + "</Widget>\n";

		return ret;
	}

	public String toCppStep(ArrayList<String> allHandles, WidgetInstance parent)
	{
		String ret = "";

		System.out.println("adding " + mName);
		allHandles.add(mName);
		ret += "\t\t" + mName + " = maWidgetCreate(" + quote(mType.getName())
			+ ");\n";
		if (parent != null)
			ret += "\t\tmaWidgetAddChild(" + parent.getName() + ", " + mName
				+ ");\n";
		for (WidgetPropertyInstance p : mProperties.values())
		{
			ret += p.toCpp();
		}

		for (WidgetInstance c : mChildren)
		{
			ret += c.toCppStep(allHandles, this);
			// ret += "\t\tmaWidgetAddChild(" + name + ", " + c.getName() +
			// ");\n";
		}
		ret += "";

		return ret;
	}

	public String toLuaCreateStep()
	{
		return mName + " = maWidgetCreate(" + quote(mType.getName()) + ")\n";
	}

	public String toLuaAddStep(WidgetInstance c)
	{
		return "maWidgetAddChild(" + mName + ", " + c.getName() + ")\n";
	}

	public String toLua()
	{
		String ret = "";
		ret += toLuaCreateStep();
		for (WidgetPropertyInstance p : mProperties.values())
		{
			ret += p.toLua();
		}

		for (WidgetInstance c : mChildren)
		{
			ret += c.toLua();
			ret += toLuaAddStep(c);
		}
		return ret;
	}

	public WidgetType getType()
	{
		return mType;
	}

	public WidgetInstance getParent()
	{
		return mParent;
	}

	public String getName()
	{
		return mName;
	}

	public void setName(String name)
	{
		this.mName = name;
	}

	public String toString()
	{
		return mType.getName() + ": " + mName;
	}

	protected String mName;
	protected WidgetType mType;
	protected Map<String, WidgetPropertyInstance> mProperties;
	protected ArrayList<WidgetInstance> mChildren;
	protected WidgetInstance mParent;
}